import { Inject, Get, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { MedicalRecordEntity } from '../../entity/record';
import { MedicalRecordService } from '../../service/record';
import { UserInfoEntity } from '../../../user/entity/info';
import { MedicalTemplateEntity } from '../../entity/template';

/**
 * 病历记录
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: MedicalRecordEntity,
  service: MedicalRecordService,
  pageQueryOp: {
    select: ['a.*', 'b.nickName', 'c.name as templateName'],
    join: [
      {
        entity: UserInfoEntity,
        alias: 'b',
        condition: 'a.studentId = b.id',
        type: 'leftJoin',
      },
      {
        entity: MedicalTemplateEntity,
        alias: 'c',
        condition: 'a.templateId = c.id',
        type: 'leftJoin',
      },
    ],
    where: async ctx => {
      const { visitTimeStart, visitTimeEnd, studentName } = ctx.request.body;
      const condition = [];
      if (visitTimeStart) {
        condition.push(['a.visitTime >= :visitTimeStart', { visitTimeStart }]);
      }
      if (visitTimeEnd) {
        condition.push(['a.visitTime <= :visitTimeEnd', { visitTimeEnd }]);
      }
      if (studentName) {
        condition.push([
          'b.nickName LIKE :studentName',
          { studentName: `%${studentName}%` },
        ]);
      }
      return condition;
    },
  },
})
export class AdminMedicalRecordController extends BaseController {
  @Inject()
  medicalRecordService: MedicalRecordService;

  
}
